﻿

//*****************说明********字符串 LED样式控件*********************************************
//**********显示的数字或字符用LEDStrValue设置******不可显示字符显示为  三 ****************
//*********当为“”时候  控件不会显示，目前这样设置起到一个隐藏功能 后期可继承或修改为特点 错误提示符*********
//*********目前设置 控件外观大小手工改变，字符的大小随个数多少的改变。字符数量适应控件大小，目前这样设置*****
//*********通过设置 MyLEDWidthMode属性 决定 当数字位数增加时候向那边扩展，向左边还是右边扩展方向
//WidthAddModeWidth是单个数字的宽度（当前没有使用当前控件宽度如***后期有需求将其改为当前控件大小有利于设计阶段可视化）
//***
//**********************************************************************************************************
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WHControlLib.Controls.LED
{

    [DefaultEvent("Click")]
    [DefaultProperty("LEDStrValue")]
    //[ToolboxItem(false)]
    public partial class WHLED : UserControl
    {

        public WHLED()
        {
            //设置双缓冲开启禁止背景擦拭
            //SetStyle(
            //         ControlStyles.OptimizedDoubleBuffer
            //         | ControlStyles.ResizeRedraw
            //         | ControlStyles.Selectable
            //         | ControlStyles.AllPaintingInWmPaint
            //         | ControlStyles.UserPaint
            //         | ControlStyles.SupportsTransparentBackColor,
            //         true);


            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 双缓冲   
            this.SetStyle(ControlStyles.ResizeRedraw, true); // 调整大小时重绘
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. 
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); // 开启控件透明

            ////背景定义为透明色   
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;


            InitializeComponent();

        }
    
        private string LedStr;
        private Rectangle MyRect = new Rectangle();
        int OldLeft;
        #region 公共属性定义
        private string _LEDStrValue = "0";
        [Category("A我的"), Description("要显示的 LED 字符串，默认，无 "), Browsable(true)]
        /// <summary>
        /// 设置LED所显示的字符串
        /// </summary>
          public string LEDStrValue
        {
            get => _LEDStrValue;
            set
            {
                if (value != _LEDStrValue)
                {
                    _LEDStrValue = value;
                    SetStrLEDValue();
                }
                
            }
        }


        private int _lEDLineWidth = 10;
        [Category("A我的"), Description(" LED字符中每一段的宽度，默认，10 "), Browsable(true)]
        public int LEDLineWidth
        {
            get => _lEDLineWidth;
            set
            {
                if (Controls.Count > 0)
                {
                    foreach (var item in Controls)
                    {
                        if (item is baseLED)
                        {
                            baseLED led = (baseLED)item;
                            led.LEDLineWidth = value;
                        }
                    }
                }

                _lEDLineWidth = value; Invalidate();
            }
        }

        private Color _lEDLineColor = Color.Black;
        [Category("A我的"), Description(" LED字符中每一段的颜色，默认，黑色 "), Browsable(true)]
        public Color LEDLineColor
        {
            get => _lEDLineColor;
            set
            {
                if (Controls.Count > 0)
                {
                    foreach (var item in Controls)
                    {
                        if (item is baseLED)
                        {
                            baseLED led = (baseLED)item;
                            led.LEDLineColor = value;
                        }
                    }
                }



                _lEDLineColor = value; Invalidate();
            }
        }

        private bool _isShowBorder;
        [Category("A我的"), Description(" 是否显示外形的边框的描边，默认，false"), Browsable(true)]
        public bool IsShowBorder
        {
            get => _isShowBorder;
            set
            {
                if (Controls.Count > 0)
                {
                    foreach (var item in Controls)
                    {
                        if (item is baseLED)
                        {
                            baseLED led = (baseLED)item;
                            led.IsShowBorder = value;
                        }
                    }
                }



                _isShowBorder = value; Invalidate();
            }
        }

        private int _borderWidth = 2;
        [Category("A我的"), Description(" 描边外形的边框宽度，默认，2，如果启用不能太大，最好是偶数 不然不美观,"), Browsable(true)]
        public int BorderWidth
        {
            get => _borderWidth;
            set
            {
                if (Controls.Count > 0)
                {
                    foreach (var item in Controls)
                    {
                        if (item is baseLED)
                        {
                            baseLED led = (baseLED)item;
                            led.BorderWidth = value;
                        }
                    }
                }

                _borderWidth = value; Invalidate();
            }
        }
        private Color _borderColor = Color.Blue;
        [Category("A我的"), Description(" 外形的边框的描边的颜色，默认，蓝色"), Browsable(true)]
        public Color BorderColor
        {
            get => _borderColor;
            set
            {
                if (Controls.Count > 0)
                {
                    foreach (var item in Controls)
                    {
                        if (item is baseLED)
                        {
                            baseLED led = (baseLED)item;
                            led.BorderColor = value;
                        }
                    }
                }
                _borderColor = value; Invalidate();
            }
        }



        public enum LEDWidthMode
        {
            NoChange,AddWidthToRight,AddWidthToLeft
        }

        private LEDWidthMode _myLEDWidthMode=LEDWidthMode.NoChange;
       [Category("A我的"), Description(" 当控件数字数量变化时 控件的外观宽度是不变还是增加，是否保持LED的大小比例，默认，控件大小不变"), Browsable(true)]
        public LEDWidthMode MyLEDWidthMode
        {
            get { return _myLEDWidthMode; }
            set { _myLEDWidthMode = value; }
        }
        private int  _widthAddModeWidth=30;
        [Category("A我的"), Description(" 当选择MyLEDWidthMode 时选择LED数字尺寸不变时控件大小随数字多少改变宽度的单个数字的宽度值，默认，30"), Browsable(true)]
        public int WidthAddModeWidth
        {
            get { return _widthAddModeWidth; }
            set { _widthAddModeWidth = value; }
        }


        #endregion

        private void SetStrLEDValue()
        {
            try

            {
             

                if (Controls.Count > 0)
                {
                
                    Controls.Clear();
                }

                MyRect = ClientRectangle;


                LedStr = _LEDStrValue;
                if (LedStr.Length > 0 && LedStr != "")
                {
                    int num = LedStr.Length;
                    int AledW = Width / num;
                    int j = 0;

                    char[] LedCharArrary = LedStr.ToCharArray();

                    switch (MyLEDWidthMode)
                    {
                        case LEDWidthMode.NoChange:
                            for (int i = 0; i < LedCharArrary.Length; i++)
                            {
                                baseLED Aled = new baseLED();
                                Aled.Name = "led" + i.ToString();

                                Aled.Height = Height;
                                Aled.IsShowBorder = _isShowBorder;
                                Aled.LEDLineWidth = _lEDLineWidth;
                                Aled.LEDLineColor = _lEDLineColor;
                                Aled.BorderColor = _borderColor;
                                Aled.Value = LedCharArrary[i];

                                Aled.Top = MyRect.Y;
                                Aled.Left = MyRect.Y + i * AledW;
                                Aled.Width = AledW;

                                Controls.Add(Aled);
                                Aled.BringToFront();

                            }


                            break;
                        case LEDWidthMode.AddWidthToRight:
                   
                            for (int i = 0; i < LedCharArrary.Length; i++)
                            {
                                baseLED Aled = new baseLED();
                                Aled.Name = "led" + i.ToString();

                                Aled.Height = Height;
                                Aled.IsShowBorder = _isShowBorder;
                                Aled.LEDLineWidth = _lEDLineWidth;
                                Aled.LEDLineColor = _lEDLineColor;
                                Aled.BorderColor = _borderColor;

                                Aled.Value = LedCharArrary[i];

                                j++;

                                Aled.Top = 0;
                                Aled.Left = WidthAddModeWidth* i;
                                Aled.Width = WidthAddModeWidth;
                                Controls.Add(Aled);
                                Aled.BringToFront();

                            }

                            Width = WidthAddModeWidth + WidthAddModeWidth * --j;

                            break;
                        case LEDWidthMode.AddWidthToLeft:
                            for (int i = 0; i < LedCharArrary.Length; i++)
                            {
                                baseLED Aled = new baseLED();
                                Aled.Name = "led" + i.ToString();

                                Aled.Height = Height;
                                Aled.IsShowBorder = _isShowBorder;
                                Aled.LEDLineWidth = _lEDLineWidth;
                                Aled.LEDLineColor = _lEDLineColor;
                                Aled.BorderColor = _borderColor;

                                Aled.Value = LedCharArrary[i];

                                j++;

                                Aled.Top = 0;
                                Aled.Left = _widthAddModeWidth * i;
                                Aled.Width = _widthAddModeWidth;
                                Controls.Add(Aled);
                                Aled.BringToFront();

                            }
                            if (OldLeft>0)
                            {
                          Left=OldLeft- WidthAddModeWidth * (j-1);
                            }

                            
                            Width = WidthAddModeWidth + WidthAddModeWidth * --j;
                       
                            //this.Anchor = AnchorStyles.Right;
                            //this.Anchor=AnchorStyles.Top| AnchorStyles.Right;

                            break;
                        default:
                            break;
                    }
                  
                          

               


                }

            }
            catch
            {
                //目前没有做处理会不显示
                MessageBox.Show("Draw LED Error");

            }




        }
        //禁止背景擦除减少闪烁
        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.ExStyle |= 0x02000000;
        //        return cp;
        //    }
        //}

        private void WHLED_Load(object sender, EventArgs e)
        {
            OldLeft = this.Left;
            SetStrLEDValue();


        }

        protected override void OnSizeChanged(EventArgs e)
        {

            SetStrLEDValue();
            base.OnSizeChanged(e);
        }

    



        ///////////////////////////////////
    }
}
